function F = eq_eqs(X, E)
% computes eqm values of the 9 endogenous variables

r = E(1);
d_r = E(2);
beta = E(3);
d_beta = E(4);
x = E(5);
eps = E(6);
lambda = E(7);
tau = E(8);
di = E(9);
DD = E(10);
II = E(11);

% V^e, V^d, V^isear, V^isell, V^ihold, B^e, B^i, S^i, H^i
% e vs d, e vs i, i vs d, i vs i
x_e_d_m = (x - eps - X(1) - X(2))/2;
x_e_d_p = (x + eps - X(1) - X(2))/2;
x_e_i_m = (x - eps - X(1) - X(4))/2;
x_e_i_p = (x + eps - X(1) - X(4))/2;
x_i_d = (X(5) - X(2) - X(3))/2;
x_i_i = (X(5) - X(3) - X(4))/2;

idx = zeros(1,6); % is meeting -> contract

if x_e_d_m >= 0
    idx(1) = 1;
end
if x_e_d_p >= 0
    idx(2) = 1;
end
if x_e_i_m >= 0
    idx(3) = 1;
end
if x_e_i_p >= 0
    idx(4) = 1;
end
if x_i_d >= 0
    idx(5) = 1;
end
if x_i_i >= 0
    idx(6) = 1;
end

%% eqm
% V^e, V^d, V^isear, V^isell, V^ihold,      B^h, B^i, S^i, H^i

S = DD + X(8); % mass of sellers = developers (DD) + selling investors
B = X(6) + X(7); % mass of buyers = end-users searching + searching investors

% shortcuts for shares
s_d = DD/S; % developers among sellers
b_e = X(6)/B; % end-users among buyers

% equations

% value functions
% V^e
F(1) = - r * X(1) + beta + matching('B', S, B) * (s_d * (.5*max(0, x_e_d_m) + .5*max(0, x_e_d_p)) + ...
    (1-s_d)* (.5*max(0, x_e_i_m) + .5*max(0, x_e_i_p)));
% V^d
F(2) = - r * X(2) + beta + d_beta + matching('S', S, B) * ( b_e * (.5*max(0, x_e_d_m) + .5*max(0, x_e_d_p)) + ...
     (1-b_e) * idx(5) * x_i_d);
% V^isear
F(3) = - r * X(3) + beta + di + matching('B', S, B) * ( s_d * idx(5) * x_i_d  + (1-s_d) * idx(6) * x_i_i );
% V^isell
F(4) = - (r+d_r) * X(4) + beta + d_beta + matching('S', S, B) * (b_e * (.5*max(0, x_e_i_m) + .5*max(0, x_e_i_p)) + ...
     (1-b_e) * max(0, x_i_i));
% V^ihold
F(5) = - r * X(5) + beta + d_beta + lambda * (X(4) - X(5));

% flow conditions: inflow - outflow=0
% end-users
F(6) = ((1-X(6)) * tau - X(6) * matching('B', S, B) * (s_d * (0.5 * idx(1) + ... 
    0.5 * idx(2)) + (1-s_d) * (0.5 * idx(3) + 0.5 * idx(4)))) * 100;
% investors selling
F(7) = (X(9) * lambda - X(8) * matching('S', S, B) * (b_e * (.5*idx(3)+.5*idx(4))   + (1-b_e)*idx(6)  )) * 100;
% holding investors
F(8) =  (X(7) * matching('B', S, B) * ( s_d * idx(5)   + (1-s_d)*idx(6) ) - X(9) * lambda) * 100;

% investors' masses sum up to II
F(9) = (X(7)+X(8)+X(9) - II)*1000;
